package org.cug.controller;

import lombok.extern.slf4j.Slf4j;
import org.cug.pojo.GraduateTraining;
import org.cug.pojo.Result;
import org.cug.service.GraduateTrainingService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/graduateTraining")
/**
 * 该控制器用于处理研究生培养相关的代码的增删改查，操纵的数据库为graduate_training
 * state:已测验
 */
public class GraduateTrainingController {
    @Autowired
    private GraduateTrainingService graduateTrainingService;

    @PostMapping
    public Result add(@RequestBody GraduateTraining graduateedu){
        log.info("增加研究生培养信息{}", graduateedu);
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        graduateedu.setUserId(userId);
        graduateTrainingService.add(graduateedu);
        return Result.success();
    }

    @DeleteMapping("/id")
    public Result delete(@PathVariable Integer id){
        log.info("删除研究生培养信息{}", id);
        graduateTrainingService.delete(id);
        return Result.success();
    }

    @PutMapping
    public Result alterbyid(@RequestBody GraduateTraining graduateedu) {
        log.info("修改研究生培养信息{}", graduateedu);
        graduateTrainingService.alterbyid(graduateedu);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectid(@PathVariable Integer id) {
        log.info("查询id{}", id);
        GraduateTraining graduateedu = graduateTrainingService.selectid(id);
        return Result.success(graduateedu);
    }
    @GetMapping
    public Result list(@RequestBody(required = false) Short semester) {
        log.info("查询所有研究生培养信息");
        // 本地线程获取
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<GraduateTraining> graduateedus = graduateTrainingService.list(userId,semester);
        return Result.success(graduateedus);
    }
}
